#include "addproduct.h"
#include "ui_addproduct.h"

#include <QDateTime>
AddProduct::AddProduct(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddProduct)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    setFixedSize(this->width(),this->height());
    ui->lineEdit_id->setFocus();
}

AddProduct::~AddProduct()
{
    delete ui;
}

void AddProduct::on_pushButton_add_clicked()    //添加商品
{

    QString strName,strSupplier,strDirector,strInputTime,strOutTime,strRemark;
    int id,amout;
    double unitPrice;
    id=ui->lineEdit_id->text().toInt();
    strName=ui->lineEdit_name->text();
    amout=ui->lineEdit_amout->text().toInt();
    unitPrice=ui->lineEdit_unitprice->text().toDouble();
    strSupplier=ui->lineEdit_supplier->text();
    strDirector=ui->lineEdit_director->text();

    QDateTime datetime =QDateTime::currentDateTime();
    QString strnowinputtime=datetime.toString("yyyy-MM-dd-HH:mm:ss");

    strInputTime=strnowinputtime;
    strOutTime=strnowinputtime;


    strRemark=ui->textEdit_remark->toPlainText();
    if(ui->lineEdit_id->text().isEmpty())
    {
        QMessageBox::information(this,"提示","id不能为空，请重新输入");
        ui->lineEdit_id->setFocus();
        return;
    }

    // 检查数据库连接是否正常
    if (!QSqlDatabase::database().isOpen())
    {
         QMessageBox::information(this, "错误", "数据库连接失败，请检查数据库服务");
         return;
    }

    QSqlQuery query;
    QString sql=QString("insert into stockdatatable (stockId,stockName,stockAmout,stockUnitPrice,supplier,director,warehouseTime,deliveTime,remarks) "
      "values ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9')")
     .arg(id)
     .arg(strName)
     .arg(amout)
     .arg(unitPrice)
     .arg(strSupplier)
     .arg(strDirector)
     .arg(strInputTime)
     .arg(strOutTime)
     .arg(strRemark);
    if(query.exec(sql))
    {
        QMessageBox::information(this,"success","商品添加成功");
        emit addSuccesseded();  //发射信号，即时更新表格
        ui->lineEdit_id->clear();
        ui->lineEdit_name->clear();
        ui->lineEdit_amout->clear();
        ui->lineEdit_director->clear();
        ui->lineEdit_supplier->clear();
        ui->lineEdit_unitprice->clear();//添加完成后清空
    }
    else
    {
        QMessageBox::information(this,"error","商品添加失败"+query.lastError().text());
    }


}

void AddProduct::on_pushButton_exit_clicked()    //关闭窗口
{
    close();
}






//
